package cn.edu.scut.sys.controller;


import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.LeaderService;
import cn.edu.scut.sys.service.StudentService;
import cn.edu.scut.sys.service.TrainScoreService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.Leader;
import cn.edu.scut.sys.vo.Student;
import cn.edu.scut.sys.vo.TrainScore;


@Controller
@RequestMapping("/trainScore")
public class TrainScoreController {

	private TrainScoreService trainScoreService;
	private LeaderService leaderService;
	private StudentService studentService;
	@Resource(name="trainScoreService")
	public void setTrainScoreService(TrainScoreService trainScoreService) {
		this.trainScoreService = trainScoreService;
	}
	
	@Resource(name="leaderService")
	public void setLeaderService(LeaderService leaderService) {
		this.leaderService = leaderService;
	}
	
	@Resource(name="studentService")
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<TrainScore> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ,HttpServletRequest req){  
		 try {  
			 	String studentId="";
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            try{
	            	if(null==req.getParameter("sstudentId")||req.getParameter("sstudentId").equals(""))
	            		studentId="null";
	            	else
	            		studentId=req.getParameter("sstudentId");
	            }
	            catch(Exception ex){
	            	ex.printStackTrace();
	            }
	            if (studentId.equals("null")||studentId==null)
	            	return trainScoreService.list(request);
	            else
	            	return trainScoreService.list(request,studentId);
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}


	@RequestMapping("/getAdd")
	public String getTrainItem(String sstudentId,HttpServletRequest request){
		
		request.setAttribute("leaderList", leaderService.getAll());
		
		if (!sstudentId.equals("null"))
			request.setAttribute("sstudentId", sstudentId);
		else{
			request.setAttribute("studentList", studentService.getAll());
		}
		return "/page/trainScoreAdd";
	}
	
	@RequestMapping("/add")
	public String add(String sstudentId,String leaderId,String score,HttpServletRequest request, String trainDate){

		String studentId="";		
		try{

			Leader leader=new Leader();
			Student student=new Student();
			TrainScore trainScore = new TrainScore();
		
			if (leaderId!=null)
				leader=leaderService.get(Long.parseLong(leaderId));
			
			if (!StringUtils.isEmpty(request.getParameter("sstudentId")))
			{
			sstudentId=request.getParameter("sstudentId").toString();
			request.setAttribute("sstudentId", sstudentId);
			student=studentService.get(Long.parseLong(sstudentId));
			}
			else{
				studentId=request.getParameter("studentId").toString();
				student=studentService.get(Long.parseLong(studentId));
			}
	

			trainScore.setLeader(leader);
			trainScore.setStudent(student);
			trainScore.setScore(Integer.parseInt(score));
			trainScore.setTrainDate(Timestamp.valueOf(trainDate + " 00:00:00"));
			trainScoreService.add(trainScore);
			request.setAttribute("status", 1);
			
		}catch(Exception ex){
			request.setAttribute("status", 0);
			
		}
		
		return "/page/trainScoreAdd";
	}
	
	
	@RequestMapping("/edit")
	public String edit( String sstudentId, Long id, HttpServletRequest request){
		if (id==null) {
			return "redirect:/page/trainScoreList.jsp";
		}
		
		if (StringUtils.isEmpty(sstudentId))
			request.setAttribute("sstudentId", sstudentId);
		
		TrainScore trainScore=trainScoreService.get(id);
		
		if (null == trainScore) {
			return "redirect:/page/trainScoreList.jsp";
		}
		
		request.setAttribute ("trainScore", trainScore);
		
		return "page/trainScoreEdit";
	}
	
	@RequestMapping("/update")
	public String update(String id,String leaderId,String studentId ,String score,String trainDate,HttpServletRequest request){
		TrainScore trainScore = new TrainScore();
		String sstudentId="";
		
		try{
			sstudentId=request.getParameter("sstudentId");
			request.setAttribute("sstudentId", sstudentId);
		trainScore.setLeader(leaderService.get(Long.parseLong(leaderId)));
		trainScore.setScore(Integer.parseInt(score));
		trainScore.setId(Long.parseLong(id));
		trainScore.setStudent(studentService.get(Long.parseLong(studentId)));
		trainScore.setTrainDate(Timestamp.valueOf(trainDate + " 00:00:00"));
		trainScoreService.update(trainScore);
		request.setAttribute("status", 1);
		
		return "/page/trainScoreEdit";
		}
		catch(Exception e){
			request.setAttribute("status", 0); 
			return "/page/trainScoreEdit";
		}
		
	}
	
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
	//	trainScoreService.delTrainScore(id);
	return "redirect:/page/trainScoreList.jsp";
	}
}
